<?php
/**
 * 用户管理类
 *
 * @auther 大耳牛
 * 2016-12-31
 **/
namespace Xijie\Model;
use Think\Model;
class MemberModel extends Model{
	//定义表名称
	protected $tableName = 'member';
	
	/**
	 * 获取会员列表总数
	 *
	 **/
	public function getMemberListCount($where=array()){
		return $this->alias('member')->where($where)->count();
	}
	
	/**
	 * 获取会员列表
	 *
	 **/
	public function getMemberList($page, $where=array(), $orderby='member.visible desc, member.room_number/1 asc'){
		//$where['member.visible'] = 1;
		return $this->alias('member')
					->join('left join momoda_users as users on member.user_id = users.user_id')
					->field('member.*, users.nickname')
					->where($where)
					->order($orderby)
					->page($page, C('PAGE_SIZE'))
					->select();
	}
	
	public function getMemberListAll($where=array()){
		return $this->alias('member')
					->join('left join momoda_users as users on member.user_id = users.user_id')
					->field('member.*, users.nickname')
					->where($where)
					->order('member.checkin_time asc')
					->select();
	}
	
	public function getMemberListToOrder($where=array()){
		return $this->alias('member')
					->where($where)
					->order('member.checkin_time asc')
					->select();
	}

	/**
	 * 客人信息保存
	 *
	 **/
	public function addMemberInfo($data){
		$this->data($data)->add();
	}
	
	/**
	 * 删除客人信息
	 * @param int $memberId 客人ID
	 **/
	public function delMemberInfo($memberId, $state){
		return $this->where(array('member_id' => $memberId))->setField('visible', $state);
	}
	
	public function reviewMemberInfo($memberId, $state){
		return $this->where(array('member_id' => $memberId))->setField('is_review', $state);
	}

	/**
	 * 编辑客人信息
	 * @param int $memberId
	 * @param Array $data 编辑资料
	 **/
	public function editMemberInfo($memberId, $data){
		return $this->where(array('member_id' => $memberId))->save($data);
	}
	
	/**
	 * 编辑客人相关信息
	 * 
	 **/
	public function editMemberRentTimeAndPtime($where=array(), $data){
		return $this->where($where)->setField($data);
	}

	/**
	 * 查询客人详情
	 * @param int $memberId
	 **/
	public function getMemberInfoById($memberId, $visible=1){
		if($visible){
			$where['visible'] = 1;
		}
		$where['member_id'] = $memberId;
		return $this->where($where)->find();
	}
	
	/**
	 * 查询客人详情ss
	 * @param int $memberId
	 **/
	public function getMemberInfoByPhone($phone, $code, $visible=1){
		$where = array(
			'member_phone' => $phone,
			'code' => $code,
			'visible' => 1
		);
		
		if($visible==0){
			unset($where['visible']);
		}
		
		return $this->where($where)->order('member_id desc')->find();
	}
	
	/**
	 * 查询合同号是否存在
	 *
	 **/
	public function getMemberInfoByContract($contract){
		return $this->where(array(
			'contract' => $contract
		))->count();
	}
	
	/**
	 *  查询客人详情
	 *
	 **/
	public function getMemberInfoByRoomnumberAndTimes($roomnumber, $begintime, $endtime){
		return $this->alias('members')
					->join('left join momoda_room as rooms on members.room_number = rooms.room_number')
					->field('members.member_id, members.`member_nick`,members.`leaving_time`,members.`contract`,members.discount,rooms.room_number,rooms.`rent`,rooms.`property`')
					->where(array(
						'members.room_number' => $roomnumber,
						'members.visible' => 1
						//'members.checkin_time' => array('elt', $begintime),
						//'members.leaving_time' => array('egt', $endtime)
					))->limit(1)
					->find();
	}
	
	/**
	 * 查询信息
	 **/
	public function getMemberInfoByWContract($where=array()){
		return $this->alias('mb')
					->join('left join momoda_room as rooms on mb.room_number = rooms.room_number')
					->field('mb.member_id,mb.`member_nick`,mb.checkin_time,mb.point,mb.`leaving_time`,mb.`contract`,mb.discount,mb.btime,mb.etime,mb.property_time,rooms.room_number,rooms.`rent`,rooms.`property`, rooms.deposit')
					->where($where)
					->limit(1)
					->find();
	}
	
	/**
	 * 根据房间号获取信息
	 **/
	public function getMemberInfoByRoomnumber($roomnumber){
		$list = $this->where(array(
			'visible' => 1,
			'room_number' => $roomnumber
		))->order('member_id desc')->limit(1)->find();
		//echo $this->_sql();
		return $list;
	}
	
	/**
	 * 修改用户押金
	 **/
	public function setMemberDepositByUserId($userid, $deposit){
		return $this->where(array('member_id' => $userid))->setInc('deposit', $deposit);
	}

	/**
	 * 查询房间是否存在未结束的用户和未激活的合同
	 * @param string $roomnumber 房间号
	 **/
	public function getMemberInfoByRoomnumberOrderLeave($roomnumber){
		return $this->where(array(
			'room_number' => $roomnumber,
			'visible' => array('neq', 0)
		))->order('leaving_time desc')
		->limit(1)
		->find();
	}

	/**
	 * 根据合同号查询用户详细信息
	 *
	 **/
	public function getMemberInfoByContractArray($contract){
		return $this->where(array(
			'contract' => $contract
		))->find();
	}

	/**
	 * 根据条件查询目标房间在提交的时间段内是否已有预定用户
	 * @param string $roomnumber 房间号
	 **/
	public function getYuDingInfoByRoomnumber($roomnumber){
		return $this->where(array(
			'room_number' => $roomnumber,
			//'etime' = array('egt', $btime)
			//'_string' => 'btime <= '. $btime . ' and etime >= '. $etime,
			'visible' => -1
		))->order('btime asc')->select();
	}

	/**
	 * 获取合同列表
	 **/
	public function getContractListByWhere($where=array(), $page, $orderby="contract_id desc"){
		return M('contract')->where($where)->order($orderby)->page($page, C('PAGE_SIZE'))->select();
	}

	/**
	 * 获取合同数量
	 **/
	public function getContractCountByWhere($where=array()){
		return M('contract')->where($where)->count();
	}

	/**
	 * 添加合同编号
	 **/
	public function addContractInfo($data){
		return M('contract')->addAll($data);
	}

	public function deleteContractInfo($cid, $status){
		return M('contract')->where(array(
			'contract_id' => $cid
		))->setField('visible', $status);
	}
	
}
